package xyz.hubery.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.hubery.domain.SysUser;
import xyz.hubery.domain.SysUserRole;
import xyz.hubery.mapper.SysUserRoleMapper;
import xyz.hubery.service.SysUserRoleService;
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService{


    @Override
    public void addUserRoles(Long userId, List<Long> roleIds) {
        List<SysUserRole> list = new ArrayList<>();
        for (Long roleId : roleIds) {
            SysUserRole userRole = new SysUserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(userId);
            list.add(userRole);
        }
        this.saveBatch(list);
    }

    @Override
    public void addRoleOfUser(SysUser sysUser) {
        List<SysUserRole> roles = this.list(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, sysUser.getId()));
        StringBuilder sb = new StringBuilder();
        if(!roles.isEmpty()) {
            for(int i=0;i<roles.size()-1;i++) {
                sb.append(roles.get(i).getRoleId() + ",");
            }
            sb.append(roles.size()-1);
        }
        sysUser.setRole_strings(sb.toString());
    }
}
